package com.mairuide.phone.phonetitle;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.Constant;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide._frame.utils.Tools;
import com.mairuide.datalog.ActionType;
import com.mairuide.datalog.DataOperationLogService;
import com.mairuide.datalog.ModuleFlag;
import com.mairuide.datalog.SystemFlag;
import com.mairuide.platmanager.PlatManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * @Auther: zyj
 * @Date: 2019/3/28 17:10
 * @Description:
 */
@Controller
@RequestMapping("/phone/title")
public class PhoneTitleController extends BaseController {

    @Autowired
    private PhoneTitleService phoneTitleService;
    @Resource
    private DataOperationLogService dataOperationLogService;


    @ModuleCode(pValue = "phoneanalysis", value = "phoneTitle")
    @RequestMapping(value = "/pool")
    public Object getPhoneTitle(){

        return "/phonerecord/phonetitlepool";
    }

    /**
     * 根据对应代码获取列表
     * @param id
     * @return
     */
    @RequestMapping(value = "/getTitlePools",method = RequestMethod.GET)
    @ResponseBody
    public Object getTitlePools(Integer id){
        List<PhoneTitle> phoneTitle = phoneTitleService.getPhoneTitle(id);
        return phoneTitle;
    }

    /**
     * 根据id获取标题
     * @param id
     * @return
     */
    @RequestMapping(value = "/getTitlePool",method = RequestMethod.GET)
    @ResponseBody
    public Object getTitlePool(Integer id){
        PhoneTitle one = phoneTitleService.findOne(id);
        return one;
    }

    /**
     * 添加标题
     * @param phoneTitle
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/saveAdd",method = RequestMethod.POST)
    @ResponseBody
    public Object saveAddTitle(PhoneTitle phoneTitle) throws Exception{
        Integer managerId = super.getLoginUser().getId();
        Map<String ,Object> model = new HashMap<>();
        PhoneTitle queryPhoneTitle = phoneTitleService.queryPhoneTitle(phoneTitle.getTitleName());
        if (null!=queryPhoneTitle){
            model.put("msg","标题已经存在");
            if (queryPhoneTitle.getIsDelete()==Constant.DELETED){
                queryPhoneTitle.setIsDelete(Constant.NOT_DELETED);
                phoneTitleService.save(queryPhoneTitle);
                model.put("msg","添加成功");
            }
        }else {
            phoneTitle.setIsDelete(Constant.NOT_DELETED);
            phoneTitle.setCreateId(managerId);
            phoneTitle.setCreateTime(new Timestamp(new Date().getTime()));
            phoneTitleService.save(phoneTitle);
            model.put("msg","添加成功");
            dataOperationLogService.save(phoneTitle.getId().toString(), super.getLoginUser(), phoneTitle, new PhoneTitle(), SystemFlag.HD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.ADD);
        }
        //插入添加日志
        return SUCCESS(model);
    }

    /**
     * 修改标题
     * @param phoneTitle
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/saveUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object saveTitle(PhoneTitle phoneTitle) throws Exception{
        PlatManager pm = super.getLoginUser();
        Integer managerId = pm.getId();
        Map<String ,Object> model = new HashMap<>();
        PhoneTitle cpdftp = Tools.beanCopyForLodBean(phoneTitle);
        PhoneTitle one = phoneTitleService.findOne(phoneTitle.getId());
        if (managerId == 1 || one.getCreateId() == managerId){//管理员
            one.setTitleName(phoneTitle.getTitleName());
            model.put("msg","修改成功");
        }else {
            model.put("msg","没有修改此条记录权限");
        }
        //添加修改日志
        dataOperationLogService.save(phoneTitle.getId().toString(), pm, one, cpdftp, SystemFlag.HD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.MODIFY);
        return SUCCESS(model);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteTitle(Integer id, ModelMap model) throws Exception{
        PhoneTitle dftp = phoneTitleService.findOne(id);
        PhoneTitle cpdftp = Tools.beanCopyForLodBean(dftp);
        dftp.setIsDelete(Constant.DELETED);
        dftp = phoneTitleService.save(dftp);
        model.addAttribute("msg","删除成功");
        //添加删除日志
        dataOperationLogService.save(dftp.getId().toString(), super.getLoginUser(), dftp, cpdftp, SystemFlag.HD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.DELETE);
        return SUCCESS(model);
    }
}
